
package com.csc440.team2.activity;

import java.util.ArrayList;
import java.util.List;


import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;


import com.csc440.team2.SmilConstants;
import com.csc440.team2.database.entities.FolderType;
import com.csc440.team2.database.entities.Message;
import com.csc440.team2.smil.SMILMessage;
import com.csc440.team2.smil.SMILReader;


public class Inbox extends ListActivity
{

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<SMILMessage>(this, R.layout.inbox_menu, getSMILMessages()));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                // When clicked, show a toast with the TextView text
                Intent myIntent = new Intent(view.getContext(), SMILPlayer.class);
                myIntent.putExtra(SmilConstants.MESSAGE, getSMILMessages().get(position));
                startActivity(myIntent);
            }
        });

    }


    private List<SMILMessage> getSMILMessages()
    {
        List<SMILMessage> messages = new ArrayList<SMILMessage>();

        List<Message> messageList = Message.getAll(this);

        int counter = 1;
        for (Message currentMessage : messageList)
        {
            if (currentMessage.getFolder() == FolderType.INBOX)
            {
                SMILMessage smilMessage = SMILReader.parseMessage(currentMessage.getMessage());
                smilMessage.setSubject("Message " + counter);
                messages.add(smilMessage);
            }

            ++counter;
        }

        return messages;
    }

}
